package cn.wolfcode.wolf2w.destination.service.impl;

import cn.wolfcode.wolf2w.destination.api.domain.Destination;
import cn.wolfcode.wolf2w.destination.api.domain.Region;
import cn.wolfcode.wolf2w.destination.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.destination.query.DestinationQuery;
import cn.wolfcode.wolf2w.destination.service.IDestinationService;
import cn.wolfcode.wolf2w.destination.service.IRegionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 目的地Service业务层处理
 * 
 * @author dafei
 * @date 2023-11-21
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper,Destination> implements IDestinationService {

    @Autowired
    private IRegionService regionService;



    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());

       /* LambdaQueryWrapper<Destination> wrapper = new LambdaQueryWrapper<>();
        if(qo.getParentId() != null){
            wrapper.eq(Destination::getParentId, qo.getParentId());
        }else{
            wrapper.isNull(Destination::getParentId);
        }
        super.page(page, wrapper);*/
        return lambdaQuery()
                .eq(qo.getParentId() != null, Destination::getParentId, qo.getParentId())
                .isNull(qo.getParentId() == null, Destination::getParentId)
                .page(page);
    }



    @Override
    public List<Destination> queryByRid(Long rid) {
        Region region = regionService.getById(rid);
        String refIds = region.getRefIds();
        String[] split = refIds.split(",");
        List<Long> ids = Arrays.stream(split).map(Long::parseLong).collect(Collectors.toList());
        return super.listByIds(ids);
    }

    @Override
    public List<Destination> queryByRidForPC(Long rid) {
        //1>查询指定rid 的目的地集合
        List<Destination> list = null;
        if(rid == -1){
            //国内
            list = lambdaQuery().eq(Destination::getParentName, "中国").list();
        }else{
            list = this.queryByRid(rid);
        }
        //2>遍历查询目的地集合下所有子目的地
        for (Destination dest : list) {
            List<Destination> children = lambdaQuery()
                    .eq(Destination::getParentId, dest.getId())
                    .last(" limit 5")
                    .list();
            dest.setChildren(children);
        }
        return list;
    }

    // 根》》 中国》》 广东》》 广州
    @Override
    public List<Destination> queryToasts(Long destId) {
        List<Destination> list = new ArrayList<>();
        //方案1：使用while 当parentId == null 时停止--自己写
        //方案2：使用递归： 自己调用自己
        this.createToasts(list, destId);

        Collections.reverse(list);  // ABC -> CBA

        return list;
    }

    private void createToasts(List<Destination> list, Long destId){
        if(destId == null){
            return;
        }
        Destination dest = super.getById(destId);
        list.add(dest);
        if(dest.getParentId() != null){
            this.createToasts(list, dest.getParentId());
        }
    }


}
